//
//  MusicListTableViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/30.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "MusicListTableViewCell.h"

@interface MusicListTableViewCell()
// 曲目图
@property(nonatomic,strong)UIImageView *fmImageView;
// 曲目图上的播放图
@property(nonatomic,strong)UIImageView *fmPlayImageView;
// 曲目图上的暂停图
@property(nonatomic,strong)UIImageView *fmPauseImageView;
// 标题
@property(nonatomic,strong)UILabel *fmTitleLabel;

// 播放数量的图标
@property(nonatomic,strong)UIImageView *fmPlayCountImageView;
// 播放数量
@property(nonatomic,strong)UILabel *fmPlayCountLabel;
// 播放时长的图标
@property(nonatomic,strong)UIImageView *fmTimerImageView;
// 播放时长
@property(nonatomic,strong)UILabel *fmTimerLabel;
// 分割线
@property(nonatomic,strong)UIImageView *fmLineImageView;
@end

@implementation MusicListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fmImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmImageView];
        
        self.fmPlayImageView = [[UIImageView alloc] init];
        [self.fmImageView addSubview:_fmPlayImageView];
       
        self.fmPauseImageView = [[UIImageView alloc] init];
        [self.fmImageView addSubview:_fmPauseImageView];
        
        self.fmTitleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmTitleLabel];
        
        self.fmPlayCountImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmPlayCountImageView];
        
        self.fmPlayCountLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmPlayCountLabel];
        
        self.fmTimerImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmTimerImageView];
        
        self.fmTimerLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmTimerLabel];
        
        self.fmLineImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmLineImageView];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 曲目图
    self.fmImageView.frame = CGRectMake(FM_Screen_Pts_Width*10, FM_Screen_Pts_Height*10, FM_Screen_Pts_Width*70, FM_Screen_Pts_Width*70);
    self.fmImageView.layer.cornerRadius = FM_Screen_Pts_Width*35;
    self.fmImageView.layer.masksToBounds = YES;
    
    // 曲目图上的播放图
    self.fmPlayImageView.frame = CGRectMake(FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    // 曲目图上的暂停图
    self.fmPauseImageView.frame = CGRectMake(FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    
    // 标题
    self.fmTitleLabel.frame = CGRectMake(FM_Screen_Pts_Width*90, FM_Screen_Pts_Height*15, FM_Screen_Width-FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmTitleLabel.font = [UIFont systemFontOfSize:15];
    self.fmTitleLabel.numberOfLines = 2;
    [self.fmTitleLabel sizeToFit];
    
    // 播放次数图标
    self.fmPlayCountImageView.frame = CGRectMake(FM_Screen_Pts_Width*90, self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15);
    // 播放次数
    self.fmPlayCountLabel.frame = CGRectMake(FM_Screen_Pts_Width*108, self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20-FM_Screen_Pts_Height*6, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmPlayCountLabel.font = [UIFont systemFontOfSize:13];
    self.fmPlayCountLabel.textColor = [UIColor grayColor];
    
    // 播放时长图标
    self.fmTimerImageView.frame = CGRectMake(FM_Screen_Pts_Width*200,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15);
    // 播放时长
    self.fmTimerLabel.frame = CGRectMake(FM_Screen_Pts_Width*218,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20-FM_Screen_Pts_Height*6, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmTimerLabel.font = [UIFont systemFontOfSize:13];
    self.fmTimerLabel.textColor = [UIColor grayColor];
    
    // 分割线
    self.fmLineImageView.frame =CGRectMake(0, 0, FM_Screen_Width, 1);
    
}


// 重写set方法
- (void)setBodyDictionary:(NSDictionary *)bodyDictionary
{
    if (_bodyDictionary != bodyDictionary) {
        _bodyDictionary = bodyDictionary;
        [self setBodyInfo:bodyDictionary];
    }
}

// 设置曲目列表数据
- (void)setBodyInfo:(NSDictionary *)bodyDictionary
{
    
    NSString *urlStr = [bodyDictionary objectForKey:@"coverMiddle"];
    if ([urlStr isEqualToString:@""] || urlStr == nil) {
        urlStr = [bodyDictionary objectForKey:@"smallLogo"];
    }
    NSURL *imageViewUrl = [NSURL URLWithString:urlStr];
    
    [self.fmImageView sd_setImageWithURL:imageViewUrl placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
    
    self.fmPlayImageView.image = [UIImage imageNamed:@"cell_sound_play_n@2x.png"];
    self.fmPauseImageView.image = [UIImage imageNamed:@"cell_sound_pause_n@2x.png"];
    
    self.fmTitleLabel.text = [bodyDictionary objectForKey:@"title"];
    
    self.fmPlayCountImageView.image = [UIImage imageNamed:@"sound_playtimes@3x.png"];

    CGFloat playCount = [[bodyDictionary objectForKey:@"playtimes"] floatValue];
    if ((playCount / 10000) > 1) {
        self.fmPlayCountLabel.text = [NSString stringWithFormat:@"%.2f 万",(playCount / 10000)];
    }else{
        self.fmPlayCountLabel.text = [NSString stringWithFormat:@"%.0f", playCount];
    }
    
    self.fmTimerImageView.image = [UIImage imageNamed:@"search_hist_tag@2x.png"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"mm:ss";
    NSTimeInterval longTimer = [[bodyDictionary objectForKey:@"duration"] doubleValue];
    NSDate *totelDate = [NSDate dateWithTimeIntervalSince1970:longTimer];
    self.fmTimerLabel.text = [formatter stringFromDate:totelDate];
    
    self.fmLineImageView.image = [UIImage imageNamed:@"cell_contactlist_line@2x.png"];
}


@end
